/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package carrello.impl.boundary.registereduser;

import java.util.Date;
import carrello.model.association.Invoice;
import carrello.impl.boundary.AbstractUI;
import carrello.impl.controller.registereduser.ShoppingCartCTRL;
import carrello.impl.controller.user.LogInCTRL;
import carrello.model.entities.PaymentProcessor;
import carrello.model.entities.Product;
import carrello.model.entities.User;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import static carrello.util.EntityManagerUtil.getEntityManager;
/**
 *
 * @author mepcotterell
 */
public class CheckoutUI extends AbstractUI {

    private Map pmap;
    
    private int[] productid;
    private int[] quantity;
    private int success;
    
    public void setsuccess ( int s ) {
        success = s;
    }
    public void setproductid ( int[] p ) {
        productid = p;
    }
    
    public int[] getproductid () {
        return productid;
    }

    public int[] getQuantity() {
        return quantity;
    }

    public void setQuantity(int[] quantity) {
        this.quantity = quantity;
    }

    public Map<Product, Integer> getPmap() {
        return pmap;
    }

    public void setPmap(Map<Product, Integer> pmap) {
        this.pmap = pmap;
    }

    @Override
    public String execute()
    {
        // If the user is not logges in then they shouldn't be here...
        if ( !getSessionIsLoggedIn()) {
            return LOGIN;
        } // if
        
        if ( success == 1 ){
            User u = User.getUserByUserName(getSessionUsername());
            Map<Integer, Integer> cart = getSessionCart ();
            
            ShoppingCartCTRL shop_ctrl = new ShoppingCartCTRL();
            shop_ctrl.saveInvoice ( cart, u, success);
            setSessionCart(new HashMap<Integer, Integer>());
            return "congrats";
        }
        
        Map<Integer,Integer> cartMap = getSessionCart();
        if (getCartCount() == 0) {
            return ERROR;
        } else if(quantity != null) {
            for ( int i = 0; i < quantity.length; i ++) {
                if (quantity[i] != 0) {
                    cartMap.put(productid[i], quantity[i]);
                    viewCart(productid[i], quantity[i]);
                } else {
                    cartMap.remove(productid[i]);
                }
            }
            setSessionCart(cartMap);
            if (getSessionCart().isEmpty()) {
                return ERROR;
            } // end if
            return SUCCESS;
        } else {
            Set<Map.Entry<Integer,Integer>> carte = cartMap.entrySet();
            for (Map.Entry<Integer, Integer> cart : carte) {
                viewCart(cart.getKey(), cart.getValue());
            } // for
        } // if
        
        return INPUT;
        
    }
    
    public void viewCart(Integer productId, Integer quantity)
    {
        ShoppingCartCTRL shop_ctrl = new ShoppingCartCTRL();
        Map<Product, Integer> map = shop_ctrl.getProductInfo(productId, quantity);
        if (pmap == null) {
            pmap = new HashMap<Product, Integer>();
            pmap.putAll(map);
        } // if
        pmap.putAll(map);
    }

    
}
